package com.milk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.milk.entity.Category;
import com.milk.entity.Milktea;
import com.milk.entity.Orders;
import com.milk.entity.User;
import com.milk.mapper.CategoryMapper;
import com.milk.mapper.MilkteaMapper;
import com.milk.mapper.OrdersMapper;
import com.milk.mapper.UserMapper;
import com.milk.service.WorkspaceService;
import com.milk.vo.BusinessDataVO;
import com.milk.vo.OrderOverViewVO;
import com.milk.vo.StatusOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MilkteaMapper milkteaMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public BusinessDataVO businessData(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        QueryWrapper<Orders> qw = new QueryWrapper<>();
        qw.ge("order_time",beginTime);
        qw.le("order_time",endTime);
        List<Orders> list = ordersMapper.selectList(qw);

        //计算营业额
        double sum = list.stream().mapToDouble(Orders::getAmount).sum();
        //计算平均客单价
        double unitPrice = list.isEmpty() ? 0.0 : sum / (list.size()*1.0);

        //查看今日新增客户
        QueryWrapper<User> userqw = new QueryWrapper<>();
        qw.ge("create_time",beginTime);
        qw.le("create_time",endTime);
        Long newUsers = userMapper.selectCount(userqw);

        //计算订单总数
        int size = list.size();

        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setTurnover(sum);
        businessDataVO.setUnitPrice(unitPrice);
        businessDataVO.setNewUsers(newUsers);
        businessDataVO.setOrderSum(size);
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    public OrderOverViewVO overviewOrders() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        QueryWrapper<Orders> qw = new QueryWrapper<>();
        //制作中数量
        int making = 0;
        //已完成数量
        int completedOrders = 0;
        //总数
        int allOrders = 0;

        List<Orders> makingOrders = new ArrayList<>();
        List<Orders> list = ordersMapper.selectList(qw);
        for (Orders orders : list) {
            if (orders.getStatus()==1) {
                making++;
                makingOrders.add(orders);
            }
            if (orders.getStatus() == 2) completedOrders++;
        }
        allOrders = making + completedOrders;
        return new OrderOverViewVO(making,completedOrders,allOrders,makingOrders);
    }

    /**
     * 查询奶茶总览
     * @return
     */
    public StatusOverViewVO overviewMilktea() {
        QueryWrapper<Milktea> qw = new QueryWrapper<>();
        qw.eq("status",0);
        Long discontinued = milkteaMapper.selectCount(qw);
        qw = new QueryWrapper<>();
        qw.eq("status",1);
        Long sold = milkteaMapper.selectCount(qw);
        return new StatusOverViewVO(sold,discontinued);
    }

    /**
     * 查询分类总览
     * @return
     */
    public StatusOverViewVO overviewCategory() {
        QueryWrapper<Category> qw = new QueryWrapper<>();
        qw.eq("status",0);
        Long discontinued = categoryMapper.selectCount(qw);
        qw = new QueryWrapper<>();
        qw.eq("status",1);
        Long sold = categoryMapper.selectCount(qw);
        return new StatusOverViewVO(sold,discontinued);
    }
}
